package androidgames.data;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class Handler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean systemFile = false;
	private boolean fileElement = false;
	private ArrayList<gameFile> listFile;
	private gameFile file;
	private ParsedDataSet myParsedDataSet = new ParsedDataSet();

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ParsedDataSet getParsedData() {
		return this.myParsedDataSet;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedDataSet = new ParsedDataSet();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (listFile == null) {
			listFile = new ArrayList<gameFile>();
		}
		if (localName.equals("systemFile")) {
			this.systemFile = true;
		} else if (localName.equals("file")) {
			file = new gameFile();
			String ID = atts.getValue("id");
			String Name = atts.getValue("name");
			String Ver = atts.getValue("ver");
			String Path = atts.getValue("path");
			String svPath = atts.getValue("url");

			file.id = ID;
			file.name = Name;
			file.version = Ver;
			file.filePath = Path;
			file.downloadPath = svPath;
			listFile.add(file);

		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("systemFile")) {
			this.systemFile = false;
		}
		if (localName.equals("file")) {
			this.fileElement = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		// if (this.in_mytag) {
		// myParsedDataSet.setExtractedString(new String(ch, start, length));
		// }
	}

	public ArrayList<gameFile> getListFile() {
		return listFile;
	}
}